/**
 *	Contains the validator object and code
**/

"use strict"

if (typeof GameLogic === "undefined") {
	var GameLogic = { }
}

GameLogic.validator = {
	
	// contains all the rules
	_rules: { },
	
	// rules modules (seperate files) can add rules using this function
	addRules: function(rules) {
		for (var r in rules) {
			if (typeof this._rules[r] !== "undefined") {
				throw "winChecker addrules: rule '" + r + "' already defined"
			}
			this._rules[r] = rules[r]
		}
	},

	//validator main function
	validate: function(game, move) {
		
		// run the validators in this order!
		var validators = game.rules
		
		// loop through victory contitions looking for winner
		var result
		for (var i=0, ii=validators.length; i<ii; i++) {
			result = this._rules[ validators[i] ](game, move)
			if (result.success == false) {
				return result
			}
		}
		
		// must be a valid move at this point
		return { success: true }
	},
	
}
